//
// Source code recreated from a .class file by IntelliJ IDEA
// (powered by FernFlower decompiler)
//

package corntab;

import java.util.Calendar;
import java.util.Date;

public class CalendarBuilder {
    CalendarBuilder() {
    }

    public static Date buildCalendar(CrontabEntry ceb) {
        Date now = new Date(System.currentTimeMillis());
        return buildCalendar(ceb, now);
    }

    public static Date buildCalendar(CrontabEntry ceb, Date startDate) {
        Calendar after = Calendar.getInstance();
        after.setTime(startDate);
        int monthGap = _$49725(ceb.getBMonths(), after.get(2));
        Date tempDate;
        if (monthGap > 0) {
            tempDate = _$9720(0, 0, 0, 1, after.get(2) + monthGap, after.get(1));
            return buildCalendar(ceb, tempDate);
        } else if (monthGap < 0) {
            tempDate = _$9720(0, 0, 0, 1, after.get(2) + monthGap, after.get(1) + 1);
            return buildCalendar(ceb, tempDate);
        } else {
            int dayGap = _$49725(ceb.getBDaysOfMonth(), after.get(5) - 1);
            int tempDay = after.get(5) + dayGap;
            if (dayGap > 0) {
                if (!_$49729(tempDay, after.get(2), after.get(1))) {
                    after.add(2, 1);
                    tempDate = _$9720(0, 0, 0, 1, after.get(2), after.get(1));
                    return buildCalendar(ceb, tempDate);
                } else {
                    tempDate = _$9720(0, 0, 0, tempDay, after.get(2), after.get(1));
                    return buildCalendar(ceb, tempDate);
                }
            } else if (dayGap < 0) {
                after.add(2, 1);
                if (!_$49729(tempDay, after.get(2), after.get(1))) {
                    after.add(2, 1);
                    tempDate = _$9720(0, 0, 0, 1, after.get(2), after.get(1));
                    return buildCalendar(ceb, tempDate);
                } else {
                    tempDate = _$9720(0, 0, 0, tempDay, after.get(2), after.get(1));
                    return buildCalendar(ceb, tempDate);
                }
            } else {
                int weekGap = _$49725(ceb.getBDaysOfWeek(), after.get(7) - 1);
                if (weekGap > 0) {
                    after.add(7, weekGap);
                    tempDate = _$9720(0, 0, 0, after.get(5), after.get(2), after.get(1));
                    return buildCalendar(ceb, tempDate);
                } else if (weekGap < 0) {
                    after.add(4, 1);
                    after.add(7, weekGap);
                    tempDate = _$9720(0, 0, 0, after.get(5), after.get(2), after.get(1));
                    return buildCalendar(ceb, tempDate);
                } else {
                    int hourGap = _$49725(ceb.getBHours(), after.get(11));
                    int tempHour = after.get(11) + hourGap;
                    if (hourGap > 0) {
                        tempDate = _$9720(0, 0, tempHour, after.get(5), after.get(2), after.get(1));
                        return buildCalendar(ceb, tempDate);
                    } else if (hourGap < 0) {
                        after.add(5, 1);
                        tempDate = _$9720(0, 0, tempHour, after.get(5), after.get(2), after.get(1));
                        return buildCalendar(ceb, tempDate);
                    } else {
                        int minuteGap = _$49725(ceb.getBMinutes(), after.get(12));
                        int tempMinute = after.get(12) + minuteGap;
                        if (minuteGap > 0) {
                            tempDate = _$9720(0, tempMinute, after.get(11), after.get(5), after.get(2), after.get(1));
                            return buildCalendar(ceb, tempDate);
                        } else if (minuteGap < 0) {
                            after.add(11, 1);
                            tempDate = _$9720(0, tempMinute, after.get(11), after.get(5), after.get(2), after.get(1));
                            return buildCalendar(ceb, tempDate);
                        } else {
                            int secondGap = _$49725(ceb.getBSeconds(), after.get(13));
                            int tempSecond = after.get(13) + secondGap;
                            if (secondGap > 0) {
                                tempDate = _$9720(tempSecond, after.get(12), after.get(11), after.get(5), after.get(2), after.get(1));
                                return buildCalendar(ceb, tempDate);
                            } else if (secondGap < 0) {
                                after.add(12, 1);
                                tempDate = _$9720(tempSecond, after.get(12), after.get(11), after.get(5), after.get(2), after.get(1));
                                return buildCalendar(ceb, tempDate);
                            } else {
                                return after.getTime();
                            }
                        }
                    }
                }
            }
        }
    }

    private static Date _$9720(int seconds, int minutes, int hour, int dayOfMonth, int month, int year) {
        Date var7;
        try {
            Calendar cl = Calendar.getInstance();
            cl.set(year, month, dayOfMonth, hour, minutes, seconds);
            cl.set(14, 0);
            var7 = cl.getTime();
            return var7;
        } catch (Exception var8) {
            var8.printStackTrace();
            var7 = null;
            return var7;
        }
    }

    private static int _$49725(boolean[] array, int start) throws IllegalArgumentException {
        int i;
        for(i = start; i < array.length; ++i) {
            if (array[i]) {
                return i - start;
            }
        }

        for(i = 0; i < start; ++i) {
            if (array[i]) {
                return i - start;
            }
        }

        throw new IllegalArgumentException("No match Element!");
    }

    private static boolean _$49729(int day, int month, int year) {
        try {
            Calendar cl = Calendar.getInstance();
            cl.setLenient(false);
            cl.set(5, day);
            cl.set(2, month);
            cl.set(1, year);
            cl.getTime();
            return true;
        } catch (IllegalArgumentException var5) {
            boolean var4 = false;
            return var4;
        }
    }
}
